package com.lck.tutor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lck.tutor.common.BaseResponse;
import com.lck.tutor.common.ErrorCode;
import com.lck.tutor.common.Result;
import com.lck.tutor.exception.BusinessException;
import com.lck.tutor.model.domain.Question;
import com.lck.tutor.model.domain.User;
import com.lck.tutor.model.dto.QuestionQuery;
import com.lck.tutor.service.QuestionService;
import com.lck.tutor.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author:鹿存岿
 * @date 2022/10/20 15:56
 */

@RestController
@RequestMapping("/question")
public class QuestionController {

    @Resource
    private QuestionService questionService;

    @Resource
    private UserService userService;

    @GetMapping("/list")
    public BaseResponse<List<Question>> list(QuestionQuery questionQuery){
        if (questionQuery == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("questionType",questionQuery.getQuestionType());
        wrapper.eq("belongToUnit",questionQuery.getBelongToUnit());
        return Result.success(questionService.list(wrapper));
    }

    @PostMapping("/submitAns")
    public BaseResponse<String> submitAns(@RequestBody Question question, HttpServletRequest request){
        if (question == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        return questionService.submitAns(question,loginUser);
    }
}
